

/**
 * 每三位默认加,格式化
 */
function addCommas(x) {
    if (isNaN(x)) {
        return '-';
    }
    x = (x + '').split('.');
    return x[0].replace(/(\d{1,3})(?=(?:\d{3})+(?!\d))/g, '$1,') + (x.length > 1 ? '.' + x[1] : '');
}

function toCamelCase(str, upperCaseFirst) {
    str = (str || '').toLowerCase().replace(/-(.)/g, function (match, group1) {
        return group1.toUpperCase();
    });
    if (upperCaseFirst && str) {
        str = str.charAt(0).toUpperCase() + str.slice(1);
    }
    return str;
}

const replaceReg = /([&<>"'])/g;
const replaceMap = {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    '\'': '&#39;'
};

function encodeHTML(source) {
    return source == null ? '' : (source + '').replace(replaceReg, function (str, c) {
        return replaceMap[c];
    });
}

const TPL_VAR_ALIAS = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

const wrapVar = function (varName, seriesIdx?) {
    return '{' + varName + (seriesIdx == null ? '' : seriesIdx) + '}';
};

function pad(str, len) {
    str += '';
    return '0000'.substr(0, len - str.length) + str;
}

/**
 * Capital first
 */
function capitalFirst(str) {
    return str ? str.charAt(0).toUpperCase() + str.substr(1) : str;
}

export {
    addCommas,
    toCamelCase,
    encodeHTML,
    capitalFirst
};

